package com.scalar.chiptrack.sales.actions.quote;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.ChipTracActionCommand;
import com.scalar.chiptrack.utils.ISubAction;
import com.scalar.chiptrack.commons.forms.ResultFormBean;
import com.scalar.chiptrack.commons.forms.BaseResultActionForm;
import com.scalar.chiptrack.commons.bean.IPaginator;
import com.scalar.chiptrack.commons.bean.IResultBean;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.sql.Connection;

/**
 * User: ganesh
 * Date: May 13, 2008
 * Time: 5:14:50 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class PreviousQuotationLatestPriceBaseSearchAction  extends ChipTracActionCommand {
    public static final String ASC_SORT_ORDER = "ASC";
    public static final int STARTING_PAGE = 1;
    public static final int DEFAULT_PAGINATION_SIZE = 10;

    public ActionForward showResults(Integer pageNo, ResultFormBean sortBean,
                                         ActionMapping mapping,
                                         ActionForm form, HttpServletRequest request)throws Exception{
        setResults(pageNo,sortBean,mapping,form,request);


        return initFilters(mapping,form,request);
    }

    public abstract ActionForward initFilters(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request);

    public boolean setResults(Integer pageNo, ResultFormBean sortBean,
                                  ActionMapping mapping,
                                  ActionForm form,HttpServletRequest request)throws Exception{
        boolean resultsSet = false;
        BaseResultActionForm formBean=(BaseResultActionForm)form;
        IPaginator paginator=formBean.getPaginator();
        IResultBean results=null;
        Connection conn=null;
        HttpSession session;
        try{
            //conn= EhmsDataSource.getConnection();
            if(sortBean!=null){
            results = paginator.getPage(sortBean);
            pageNo = new Integer(formBean.getCurrentPageNo());
            }
            else{
            results = paginator.getPage(pageNo.intValue());
            formBean.setCurrentPageNo(pageNo.toString());
            }
            int totalPages =0 ;
            if(results!=null)
            {
                 totalPages = results.getNumberOfPages();
            }    
            if (totalPages > 0)
            {
                request.setAttribute(SessionKeys.SEARCH_RESULTS,results);
                formBean.setPageResultBean(results);
                resultsSet = true;
            }
        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("Error in Base action command setResults");
        }

        return resultsSet;
    }

    public ActionForward scroll(ActionMapping mapping,ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)throws Exception
    {
        ActionForward actionForward = null;
        BaseResultActionForm formBean= (BaseResultActionForm)form;
        String scrollAction=formBean.getScrollAction();
        int currentPageNo;
        try
        {
            currentPageNo=Integer.parseInt(formBean.getCurrentPageNo());
            if (ISubAction.RESULT_SCROLL_FIRST_ACTION.equalsIgnoreCase(scrollAction))
            {
                currentPageNo = 1;
            }
            else if (ISubAction.RESULT_SCROLL_NEXT_ACTION.equalsIgnoreCase(scrollAction))
            {

                if(currentPageNo==formBean.getPaginator().getNumberOfPages()){
                    currentPageNo=formBean.getPaginator().getNumberOfPages();
                }
                else{
                    ++currentPageNo;
                }
            }
            else if (ISubAction.RESULT_SCROLL_PREVIOUS_ACTION.equalsIgnoreCase(scrollAction))
            {
                if(currentPageNo==1){
                    currentPageNo=1;
                }
                else{
                    --currentPageNo;
                }
            }
            else if (ISubAction.RESULT_SCROLL_LAST_ACTION.equalsIgnoreCase(scrollAction))
            {
                currentPageNo = formBean.getPaginator().getNumberOfPages();
            }
            else if (ISubAction.RESULT_SCROLL_GOTO_ACTION.equalsIgnoreCase(scrollAction))
            {
            }
            //formBean.setCurrentPageNo(currentPageNo);
            actionForward=showResults(new Integer(currentPageNo),null,mapping,form,request);
        }
        catch(Exception e){
            System.out.println("Exception in scroll");
            e.printStackTrace();
        }
        return actionForward;
    }

    public ActionForward sort(ActionMapping mapping,ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)throws Exception
    {
        ActionForward actionForward = null;
        try
        {
            BaseResultActionForm formBean = (BaseResultActionForm)form;
            ResultFormBean resulthBean = formBean.getResultBean();
            resulthBean.setSearchPage(Integer.parseInt(formBean.getCurrentPageNo()));
            actionForward =showResults(null,resulthBean,mapping,form,request);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new Exception("Error occured in sort action");
        }
        return actionForward;
    }
}
